# -*- coding: utf-8 -*-
#@author: Paweł Pawłowski <abdul-pablo@tlen.pl>
#main file for gateway application with GUI
#run by command "python wsn.py"
import sys

from PyQt4 import QtCore, QtGui
from wsn_main import Ui_MainWindow
from wsn_alert import Ui_AlertDialog
from wsn_comm import WSNSerialComm

def refreshData(connection, requested_data, renew = False):
	"""Sends data request message"""
	connection.sendDataRequestMsg(requested_data_tuple = requested_data, renew_tuple = renew)

def controlValve(connection, valve):
	"""Sends control valve message"""
	connection.sendValveControlMsg(valve = valve)


class MyAlertDialog(QtGui.QDialog):
	"""Alert dialog class"""
	def __init__(self, parent=None):
		QtGui.QDialog.__init__(self, parent)
		self.ui = Ui_AlertDialog()
		self.ui.setupUi(self)

class MyWSN(QtGui.QMainWindow):
	"""Main window class"""

	def __init__(self, refreshDataCallback, controlValveCallback, parent=None):
		QtGui.QMainWindow.__init__(self, parent)
		self.ui = Ui_MainWindow()
		self.ui.setupUi(self)

		#saving callback functions
		self.refreshDataCallback = refreshDataCallback
		self.controlValveCallback = controlValveCallback

		QtCore.QObject.connect(self.ui.pushButton_Refresh, QtCore.SIGNAL("clicked()"), self.refreshData)
		QtCore.QObject.connect(self.ui.pushButton_Valve, QtCore.SIGNAL("clicked()"), self.controlValve)

		QtCore.QObject.connect(self.ui.doubleSpinBox_Water, QtCore.SIGNAL("valueChanged(double)"), self.set_progressBar_Water)
		QtCore.QObject.connect(self.ui.doubleSpinBox_Humidity, QtCore.SIGNAL("valueChanged(double)"), self.set_progressBar_Humidity)

		self.alertDialog = MyAlertDialog(self)


	def setConnectionToGatewayMote(self, connection):
		"""Stores connection object"""
		self.connection = connection

	#event handlers
	def set_progressBar_Water(self, value):
		"""Sets water level progress bar"""
		self.ui.progressBar_Water.setValue(int(value))

	def set_progressBar_Humidity(self, value):
		"""Sets humidity progress bar"""
		self.ui.progressBar_Humidity.setValue(int(value))


	def set_humidity(self, humidity = 0):
		"""Sets displayed humidity"""
		humidity = humidity / 100
		self.ui.doubleSpinBox_Humidity.setValue(humidity)

	def set_water(self, water = 0):
		"""Sets displayed water level"""
		water = water / 1000
		if water > 17:
			water = 17
		self.ui.doubleSpinBox_Water.setValue(water)

	def set_valve(self, valve = 0):
		"""Sets displayed valve state"""
		self.ui.spinBox_Valve.setValue(valve)

	def displayMsg(self, msg):
		"""Displays message in the text browser"""
		self.ui.textBrowser_Messages.append(str(msg)+'\n')

	def showAlertDialog(self, value):
		"""Displays alert"""
		msg = "\n\nAlert: " + str(value) + "\n"
		self.displayMsg(msg)

	def refreshData(self):
		"""Refreshes system state"""
		refreshValve = bool(self.ui.checkBox_ValveRefresh.checkState())
		refreshWater = bool(self.ui.checkBox_WaterRefresh.checkState())
		refreshHumidity = bool(self.ui.checkBox_HumidityRefresh.checkState())
		renewHumidity = bool(self.ui.checkBox_HumidityRenew.checkState())
		renewWater = bool(self.ui.checkBox_WaterRenew.checkState())

		requested_data = []
		renew = []
		if refreshHumidity:
			requested_data.append('humidity')
		if refreshWater:
			requested_data.append('water')
		if refreshValve:
			requested_data.append('valve')

		if renewWater:
			renew.append('water')
		if renewHumidity:
			renew.append('humidity')

		self.refreshDataCallback(connection = self.connection, requested_data = requested_data, renew = renew)

	def controlValve(self):
		"""Controls valve position"""
		valve = self.ui.spinBox_Valve.value()
		self.controlValveCallback(self.connection, valve)

if __name__ == "__main__":
	#GUI startup
	app = QtGui.QApplication(sys.argv)
	myapp = MyWSN(refreshDataCallback = refreshData, controlValveCallback = controlValve)
	myapp.show()

	#initialization of connection to serial forwarder
	serial_connection = WSNSerialComm(humidityCallback = myapp.set_humidity, waterCallback = myapp.set_water, valveCallback = myapp.set_valve, displayMsgCallback = myapp.displayMsg, alertCallback = myapp.showAlertDialog)
	myapp.setConnectionToGatewayMote(serial_connection)

	sys.exit(app.exec_())
